<?php

class Myzen_Oauth_Token_Request extends Myzen_Oauth_Token {
  
  /* definition of the requestToken table */
  static protected $_dbRequestTokenTableName = 'requestToken';
  static protected $_dbRequestTokenConsumerKeyCol = 'consumerKey';
  static protected $_dbRequestTokenKeyCol = 'key';
  static protected $_dbRequestTokenSecretCol = 'secret';
  static protected $_dbRequestTokenValidatedCol = 'validated';
  static protected $_dbRequestTokenVerifierCol = 'verifier';
  static protected $_dbRequestTokenEmailCol = 'email';
    
  protected $_consumerKey = '';  
  protected $_authorized = false;  
  protected $_verifier = '';
  protected $_email = '';
  protected $_db = null;	

  public function __construct($key) {
  	
      $db = Zend_Registry::get('dt_dbs')->getDb('auth','requestToken',$key);
      
      $this->_db = $db;
      
      $select = $db->select()->from(self::$_dbRequestTokenTableName);
      $where = $db->quoteInto(self::$_dbRequestTokenKeyCol ." = ?", $key);
      $select->where($where);
      $res = $db->fetchAll($select->__toString());  	

      if(empty($res)) throw new Myzen_Oauth_Token_Exception('request Token not found');       	
      parent::__construct($res[0][self::$_dbRequestTokenKeyCol],$res[0][self::$_dbRequestTokenSecretCol]);	
      
      $this->_consumerKey = $res[0][self::$_dbRequestTokenConsumerKeyCol];
      $this->_authorized = (bool) $res[0][self::$_dbRequestTokenValidatedCol];
      $this->_verifier = $res[0][self::$_dbRequestTokenVerifierCol];
      $this->_email = $res[0][self::$_dbRequestTokenEmailCol];
      
  }

  static public function create($consumer) {
	  
	  $consumerKey = $consumer->key;

      $requestToken = substr  (md5(time().'some random sentence'.rand().$consumer->key),12,12);
      $requestTokenSecret = md5($consumer->key.'another random sentence'.rand().time());

      $db = Zend_Registry::get('dt_dbs')->getDb('auth','requestToken',$requestToken);

      $data = array(
        self::$_dbRequestTokenConsumerKeyCol => $consumerKey,
        self::$_dbRequestTokenKeyCol => $requestToken,
        self::$_dbRequestTokenSecretCol => $requestTokenSecret
      );
      try{
	    $db->insert(self::$_dbRequestTokenTableName, $data);	    
      } catch(Zend_Db_Statement_Exception $e ) {
        Zend_Registry::get('dt_log')->log('Issue while saving request token: '.$e->getMessage(), Zend_Log::ERR);
        throw new Myzen_Oauth_Exception('Issue while saving request token');	
      }
    
    return new Myzen_Oauth_Token($requestToken,$requestTokenSecret);
  } //end create

  public function delete() {
      $db = Zend_Registry::get('dt_dbs')->getDb('auth','requestToken',$this->key);
      $n = $db->delete(self::$_dbRequestTokenTableName, $db->quoteInto(self::$_dbRequestTokenKeyCol.' = ?',$this->key));
      if('1' != $n) throw new Myzen_Oauth_Token_Exception('Issue while deleting requestToken '.$n.' Tokens deleted');
  }

  public function authorize($email) {
  	if($this->_authorized) throw new Myzen_Oauth_Token_Exception('request Token is already verified');
      $verifier = substr(md5('one more random'.time().rand()),6,12);

    $data = array(
    	self::$_dbRequestTokenValidatedCol => 1,
    	self::$_dbRequestTokenVerifierCol => $verifier,
    	self::$_dbRequestTokenEmailCol => $email
    );
    
    $n = $this->_db->update(self::$_dbRequestTokenTableName,$data, $this->_db->quoteInto(self::$_dbRequestTokenKeyCol.' = ?',$this->key));
    if(1 != $n) throw new Myzen_Oauth_Token_Exception('request token db issue: '.$n.' tokens have been modified during authorization for requestToken '.$this->key);
    
    $this->_verifier = $verifier;
    return;

  }
  
  public function isAuthorized() {
    return $this->_authorized;	
  }
  
  /* return  the token the way it should be displaye don the way back from grant process  */
  public function toGrantDetails() { 	
    return 'oauth_token='.Myzen_OAuth_Util::urlencode_rfc3986($this->key).'&oauth_verifier='.Myzen_OAuth_Util::urlencode_rfc3986($this->_verifier);
  }
  
  public function getConsumerKey() {
  	return 	$this->_consumerKey;
  }

  public function getVerifier() {
  	return 	$this->_verifier;
  }

  public function getEmail() {
  	return 	$this->_email;
  }
  
}